//
//  DFMediator.m
//  DFMediator
//
//  Created by dafiger on 2020.04.10.
//  Copyright © 2020 dafiger. All rights reserved.
//

#import "DFMediator.h"

@implementation DFMediator

//#pragma mark - 单例
//+ (DFMediator *)instance {
//    static dispatch_once_t predicate = 0;
//    static DFMediator *instance = nil;
//    dispatch_once( &predicate, ^{
//        instance = [[self alloc] init];
//    });
//    return instance;
//}
//
//- (id)init {
//    self = [super init];
//    if(self){
//
//    }
//    return self;
//}

#pragma mark - 动态注册
+ (id)moduleInstanceFromProtocol:(Protocol *)protocol {
    static NSMutableDictionary *moduleDict = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        moduleDict = [NSMutableDictionary dictionary];
    });
    
    NSString *protocolName = NSStringFromProtocol(protocol);
    
    // id moduleClass = [moduleDict objectForKey:protocolName];
    id moduleClass = moduleDict[protocolName];
    if (!moduleClass) {
        Class aClass = NSClassFromString(protocolName);
        moduleClass = [aClass new];
        // [moduleDict setObject:moduleClass forKey:protocolName];
        moduleDict[protocolName] = moduleClass;
    }
    
    if ([moduleClass conformsToProtocol:protocol]){
        return moduleClass;
    }else{
        NSLog(@"通过协议未找到 class：%@", protocolName);
        return nil;
    }
}

@end
